using UnityEngine;
using System.Collections;

public class CubeMove : MonoBehaviour
{

	// Use this for initialization	
	float sx;
	float sz;
	void Start ()
	{
		int speed = Random.Range(10,100);		
		Vector3 position = transform.position;
		float distance = Mathf.Sqrt(Mathf.Pow(480-position.x,2)
			+Mathf.Pow(320-position.z,2));
		sx = (480-position.x)*speed/distance;
		sz = (320-position.z)*speed/distance;
	}
	
	// Update is called once per frame
	void Update ()
	{
		if((transform.position.x < 482
			&& transform.position.x > 478)
			&& 
			(transform.position.z < 322
			&& transform.position.z > 318)){
			Destroy(this.gameObject);
			return;
		}
		
		transform.Translate(sx*Time.deltaTime,
			0,
			sz*Time.deltaTime,
			Space.World);
	}
}

